package com.fbs.wowo.util.upload;

import com.alibaba.fastjson.JSON;
import com.fbs.wowo.base.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class UploadController extends BaseController {

    @PostMapping("/upload")
    public void upload(MultipartFile file, String type, String name,HttpServletResponse response){
        try {
            String imgName=type+"/"+name;
            String fileUrl = Oss.getFileUrl(file, imgName, Oss.getOssEndpointPrivate(),
                    Oss.getOssAccessKeyId(), Oss.getOssAccessKeySecret(), Oss.getOssBucketName());
            response.setContentType("text/plain;charset=UTF-8");
            response.getWriter().write(JSON.toJSONString(success(fileUrl)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
